In [1]:
import os
directory = os.fsencode('Data/aligned')
files = []
for sub1 in os.listdir(directory):
sub1_name = os.fsdecode(sub1)
if '.fits'in sub1_name:
files.append('Data/aligned'+"/"+sub1_name)
We can then load the fits and keep track of the times in a dictionary. NOTE: These files are preprocessed and aligned for RA/DEC offsets
In [2]:
import os
from tqdm import tqdm
from astropy.io import fits
from astropy.time import TimeDelta, Time
import torch
import numpy as np
from astropy.io import fits
import matplotlib.pyplot as plt
from astropy.visualization import (ImageNormalize, ZScaleInterval, LogStretch, MinMaxInterval, AsinhStretch)
from astropy.wcs import WCS
from scipy.ndimage import shift
from astropy.coordinates import SkyCoord
total_data = {}
pass_counter = 0
MAGZPL_LIST = []
for i in tqdm(range(len(files))):
# Open the FITS file
filename = files[i]
hdul = fits.open(filename)
# Access the primary HDU (Header Data Unit)
primary_hdu = hdul[0]
# Get the data and header
# try:
data = primary_hdu.data
header = primary_hdu.header
total_elements = data.size
# Count the number of NaN values
nan_count = np.isnan(data).sum()
# Calculate the percentage of NaN values
nan_percentage = (nan_count / total_elements) * 100
if nan_percentage < 20:
time = header['SHUTOPEN']
MAGZPL_LIST.append(header['MAGZP'])
time = Time(time.replace('T',' '), format='iso', scale='utc')
total_data[time] = torch.tensor(data.astype(np.float32))
else:
pass_counter += 1
pass
hdul.close()
print(pass_counter, 'skipped out of ', len(files))
MAGZPL = np.max(MAGZPL_LIST)
print(MAGZPL)
100%|█████████████████████████████████████████| 939/939 [00:16<00:00, 57.27it/s]
671 skipped out of 939 26.416529
Get the earliest time of observation
In [3]:
sorted_keys = sorted(total_data.keys())
earliest_frame = sorted_keys[0]
Load the earliest data frame and plot what it looks like
In [4]:
# Apply normalization
norm = ImageNormalize(stretch=AsinhStretch(a=0.0001), interval=ZScaleInterval())
data = total_data[earliest_frame]
# norm = ImageNormalize(total_data[earliest_frame], interval=ZScaleInterval())
# Plot the image with normalization
plt.figure(figsize=(10, 8))
plt.imshow(data, cmap='gray_r', origin='lower', norm=norm)
plt.colorbar(label='Pixel Value')
plt.title('FITS Image with Normalization')
plt.xlabel('RA Pixel')
plt.ylabel('Dec Pixel')
plt.show()
To verify we have aligned the frames together we plot a movie of the frames
In [5]:
from astropy.io import fits
import matplotlib.pyplot as plt
from astropy.visualization import (ImageNormalize, ZScaleInterval, LogStretch, MinMaxInterval, AsinhStretch)
import numpy as np
import matplotlib.pyplot as plt
from celluloid import Camera
from IPython.display import HTML
norm = ImageNormalize(stretch=AsinhStretch(a=0.0001), interval=ZScaleInterval())
%matplotlib inline
# Set up the figure and axes
fig, ax = plt.subplots(figsize=(8,8))
# Initialize Celluloid Camera
camera = Camera(fig)
data = total_data[earliest_frame]
count = 0
# Animate the sine wave with a changing phase
for key in sorted_keys:
ax.imshow(total_data[key], cmap='gray_r', origin='lower', norm=norm) # Plot the sine wave
camera.snap() # Capture the frame
if count > 100:
break
else:
count += 1
# Create the animation
animation = camera.animate(interval=100) # 100ms between frames
# Display the animation
HTML(animation.to_jshtml()) # Render as JS HTML
Out[5]: